<?php

namespace App\Models\Factory;

use App\Models\AbsModelFactory;
use App\Models\Orm\Identity;


/**
 * 身份工厂
 * Class SmsFactory
 * @package App\Models\Factory
 */
class BankFactory extends AbsModelFactory
{
    public static function addBank($userId, $data)
    {
        $bankObj = Identity::where('uid', $userId)->first();

        if (empty($bankObj)) {
            $bankObj = new Identity();
        }
        $bankObj->uid = $userId;
        $bankObj->dnumber = $data['bank']['debit']['dnumber'];
        $bankObj->dname = $data['bank']['debit']['dname'];
        $bankObj->dperiod = $data['bank']['debit']['dperiod'];
        $bankObj->dbank = $data['bank']['debit']['dbank'];
        $bankObj->dphone = $data['bank']['debit']['dphone'];

        $bankObj->is_credit = $data['bank']['credit']['is_credit'];
        $bankObj->cnumber = $data['bank']['credit']['cnumber'];
        $bankObj->cname = $data['bank']['credit']['cname'];
        $bankObj->cperiod = $data['bank']['credit']['cperiod'];
        $bankObj->cbank = $data['bank']['credit']['cbank'];
        $bankObj->cphone = $data['bank']['credit']['cphone'];
        $bankObj->blass_time = date("Y-m-d h:i:s", time());
        $bankObj->bstatus = $data['bank']['bstatus'];

        if ($bankObj->save()) {
            return true;
        } else {

            return false;
        }
    }

    //获取用户银行信息
    public static function getBank($userId)
    {
        $bank = Identity::where('uid', $userId)->select('dnumber','dname','dperiod','dbank','dphone','cnumber','cname','cperiod','cbank','cphone','blass_time','is_credit')->first();
        if ($bank) {
            $bank = $bank->toArray();
            $data1['isSuccess'] = "1";
            $data1['bank']['debit']['dnumber'] = $bank['dnumber'];
            $data1['bank']['debit']['dname'] = $bank['dname'];
            $data1['bank']['debit']['dperiod'] = $bank['dperiod'];
            $data1['bank']['debit']['dbank'] = $bank['dbank'];
            $data1['bank']['debit']['dphone'] = $bank['dphone'];

            $data1['bank']['credit']['is_credit'] = $bank['is_credit'];
            $data1['bank']['credit']['cnumber'] = $bank['cnumber'];
            $data1['bank']['credit']['cname'] = $bank['cname'];
            $data1['bank']['credit']['cperiod'] = $bank['cperiod'];
            $data1['bank']['credit']['cbank'] = $bank['cbank'];
            $data1['bank']['credit']['cphone'] = $bank['cphone'];

            $data1['bank']['blass_time'] = $bank['blass_time'];
            $data1['status'] = "1";

            return $data1;
        } else {
            $data2['bank']['debit']['dnumber'] = "";
            $data2['bank']['debit']['dname'] = "";
            $data2['bank']['debit']['dperiod'] = "";
            $data2['bank']['debit']['dbank'] = "";
            $data2['bank']['debit']['dphone'] = "";

            $data2['bank']['credit']['cnumber'] = "";
            $data2['bank']['credit']['cname'] = "";
            $data2['bank']['credit']['cperiod'] = "";
            $data2['bank']['credit']['cbank'] = "";
            $data2['bank']['credit']['cphone'] = "";
            $data2['bank']['credit']['is_credit'] = 2;

            $data2['bank']['blass_time'] = "";
            $data2['status'] = "1";
            return $data2;

        }
    }
}